#include "../includes.h"


rb_	rb;


void SetRbFreq(void);
void init_rb_sro100(void);
void Init_rb(void)
{
	rb.status = RB_STATUS_NONE;
	rb.comm_ready = NO;
	rb.IntervalCounter = 0;

	rb.tx_counter = 0;
	rb.tx_flag = OFF;
	rb.tx_error = 0;
	rb.sro100.Error_counter	=	0;

	init_rb_sro100();	//sewon

	Init_OCXO_ID();
}

void init_rb_sro100(void)
{
	int i;

	rb.sro100.print_Flag		= 0;
	rb.sro100.FC_value		= 0;
	rb.sro100.prev_FC_value	= 0;

	rb.sro100.quality 		= 0;
	rb.sro100.time_interval = 0;
	rb.sro100.phase_compare = 0;
	rb.sro100.general_status= 0;

	FCsddddd(0x00);		//init
//	FSx(RB_NO_SAVE);		//EEPROM No Save
	Rb_Comm_BTx(0x00);	//init
	Rb_Comm_BTx(0x0A);	//#PTNTA 1_print/sec
//	Rb_Comm_BTx(0x0B);	//#PTNTS 1_print/sec

}

void Rb_Task()
{
	switch(rb.status){

		case RB_STATUS_NONE:
				if(CGM_P()){
					rb.status = RB_STATUS_SET;
					rb.IntervalCounter = 0;
					rb.tx_error = 0;
				}
				break;

		case RB_STATUS_SET:

				if(rb.comm_ready == YES){
					rb.status = RB_STATUS_RUN;
					rb.tx_error = 0;
				}
				else {
					// send rb command 'i'
				//	TransmitnMessage((BYTE*)"i",1,PORT_RB);
					Rb_Comm_BTx(0x0A);	//SRO-100
				}

				if(rb.IntervalCounter++ > 10){
				//	rb.status = RB_STATUS_RUN;
					//DIAG.RB = FAIL;
					rb.tx_error++;
				}

				sprintf((char*)MESG,"[%d]",rb.status);
				MessageOut(MESG);
				break;


		case RB_STATUS_RUN:

				if(rb.tx_flag){
					rb.tx_counter++;
					if(rb.tx_counter > 10){

						rb.tx_error++;

						rb.tx_flag = OFF;
						rb.tx_counter = 0;

						//DIAG.RB = FAIL;
					}
				}
				else {
					rb.tx_error = 0;
					//DIAG.RB = OK;
				}

//				if(rb.sro100.Error_counter++>60){	//sewon 12-05-21
//					DIAG.RB = FAIL;
//				}
//				else{
//					DIAG.RB = OK;
//				}

				//Setting_OCXOID();
				//OCXO_ID.Valid = YES;
				//sprintf((char*)MESG,"[%d %d %d]",rb.tx_error,rb.tx_flag,rb.tx_counter);
				//MessageOut(MESG);

				break;

	}
}


void DecodeRbCommand(BYTE *buffer,int length)
{
	switch(rb.status){

		case	RB_STATUS_SET:

					rb.status = RB_STATUS_RUN;
					if(strstr((char*)buffer,"FC: enabled, Srvc: low")){
						rb.comm_ready = YES;
					}
					break;

		case	RB_STATUS_RUN:

					if(strstr((char*)buffer,"r>")){

						rb.tx_counter = 0;
						rb.tx_flag = OFF;
					}
					break;
	}
}



//============================================================================================
//	Rubidium
//============================================================================================

void SetRbFreq(void)
{
	char message[20];

	if(OSC.kind == Rubidium_OSC){
		sprintf(message,"f%.1f\r",(float)CONTROL.RealDAC);
		TransmitnMessage((BYTE*)message,strlen(message),PORT_RB);

		if(rb.status == RB_STATUS_RUN){
			rb.tx_counter = 0;
			rb.tx_flag = ON;
		}
	}
}




//============================================================================================
//	SRO-100 Rubidium
//============================================================================================


//A: Beat Date, Time, Counter data in NMEA 0183 proprietary format
//   Answer: $PTNTA,yyyymmddhhnnss,q,T3,rrrrrrr,sfff,x,y∗cs<CR><LF>
//      (See NMEA PROPRIATARY FORMAT Chapiter)
//B: Beat Frequencies, Tra. loop parameters in NMEA 0183 proprietary format
//   Answer: $PTNTS,B,s,ffff,iiii,aaaa,x,y,s,cccccc,ggg.gg,x,y∗cs<CR><LF>
//      (See NMEA PROPRIATARY FORMAT Chapiter)
void Rb_Comm_BTx(int mode)	//Beat every second on the serial port
{
	char message[10];

	sprintf(message,"BT%x\r\n",mode);

	write(fdRbSRO,message,strlen(message));
	printf("%s",message);
}

//ddddd = (-32768	~	+32767)value
//			 (-16.7	~	+16.7) ppb
void FCsddddd(int value)	//Set user frequency correction
{
	char message[15];
	char sign;

	if( value >= 0 ) 	sign = '+';

	if( value > 30000 ) 			value = 30000;
	else if( value < -30000 ) 	value = -30000;

	if( value >= 0 )	sprintf(message,"FC%c%05d\r\n",sign,value);
	else					sprintf(message,"FC%06d\r\n",value);

	write(fdRbSRO,message,strlen(message));
	if( SYSTEM.IO.CTRL_PORT.MODE == UDEBUG ) printf(">>%s",message);
}

void FSx(int value)	//Set frequency save mode
{
	char message[10];

	sprintf(message,"FS%x\r\n",value);

	write(fdRbSRO,message,strlen(message));
	printf("%s\n",message);
}

void RbSRO_Control()	//sewon, 120503
{
	if( OSC.kind == Rubidium_OSC )//sewon rb
	{
		rb.sro100.FC_value 	= (int)( (CONTROL.RealDAC - CONFIG.DAC ) );	//CONFIG.DAC

		dbgCV(146) printf("timea.offset : %.12lf\nKGainF : %.13lf\n",timea.offset*1E9,KGainF);

		rb.sro100.FC_diff = rb.sro100.prev_FC_value - rb.sro100.FC_value;
		if( rb.sro100.FC_diff != 0 )	FCsddddd(rb.sro100.FC_value);

		rb.sro100.prev_FC_value = rb.sro100.FC_value;

		if(rb.status == RB_STATUS_RUN){
			rb.tx_counter = 0;
			rb.tx_flag = ON;
		}
	}
}

